using System;
using System.Collections.Generic;
using System.IO;
using Jitter2;
using Jitter2.Collision;
using Jitter2.Collision.Shapes;
using Jitter2.Dynamics;
using Jitter2.LinearMath;
using JitterDemo.Renderer;
using JitterDemo.Renderer.OpenGL;

namespace JitterDemo;

public class Demo23 : IDemo
{
    public string Name => "Test";
    private List<Player> players = new List<Player>();
    private World world;

    public void Build()
    {
        Playground pg = (Playground)RenderWindow.Instance;
        world = pg.World;

        pg.ResetScene();

        world.DynamicTree.Filter = World.DefaultDynamicTreeFilter;
        world.BroadPhaseFilter = new TriggerCollisionDetection(world);
        world.NarrowPhaseFilter = new TriangleEdgeCollisionFilter();
        world.Gravity = new NVector3(0, -9.81f, 0);
        world.NumberSubsteps = 1;
        world.SolverIterations = 12;

        Common.BuildJenga(new NVector3(0, 0, -20), 20, rigidBody => rigidBody.Friction = 0.3f);

        //var body = world.CreateRigidBody();
        //body.IsTriggered = true;
        //body.AddShape(new BoxShape(1, 1, 3));
        //body.Position = new NVector3(0, 1, -20);
        //body.BeginCollide += (a) =>
        //{
        //    Console.WriteLine($"Enter");
        //};
        //body.EndCollide += (a) =>
        //{
        //    Console.WriteLine($"Exit");
        //};
        for (int x = 0; x < 1; x++)
        {
            for (int y = 0; y < 1; y++)
            {
                var player = new Player(world, new NVector3(x * 3f, 5, y * 3f - 10f));
                players.Add(player);
            }
        }
    }

    public void Draw()
    {
        //var key = random.Next(0, 100);
        Keyboard kb = Keyboard.Instance;

        //if (key >= 0 && key <= 30)
        if (kb.IsKeyDown(Keyboard.Key.Left))
        {
            foreach (var player in players)
                player.SetAngularInput(-5.0f);
        }
        //else if (key >= 30 && key <= 60)
        else if (kb.IsKeyDown(Keyboard.Key.Right))
        {
            foreach (var player in players)
                player.SetAngularInput(5.0f);
        }
        else
        {
            foreach (var player in players)
                player.SetAngularInput(0.0f);
        }

        //if (key >= 60 && key <= 75)
        if (kb.IsKeyDown(Keyboard.Key.Up))
        {
            foreach (var player in players)
                player.SetLinearInput(-NVector3.UnitZ * 1f);
        }
        //else if (key >= 75 && key <= 95)
        else if (kb.IsKeyDown(Keyboard.Key.Down))
        {
            foreach (var player in players)
                player.SetLinearInput(NVector3.UnitZ * 1f);
        }
        else
        {
            foreach (var player in players)
                player.SetLinearInput(NVector3.Zero);
        }

        //if (key >= 95 && key <= 100)
        if (kb.IsKeyDown(Keyboard.Key.Q))
        {
            foreach (var player in players)
                player.Jump();
        }

        //writeStream ??= new StreamWriter(new FileStream(Environment.CurrentDirectory + "\\frame.txt", FileMode.Truncate, FileAccess.ReadWrite));
        //writeStream.WriteLine($"frame:{world.Frame}");
        //foreach (RigidBody body in world.RigidBodies)
        //{
        //    writeStream.WriteLine($"ID:{body.RigidBodyId} Pos:{body.Position} Vel:{body.Velocity}");
        //}

        //Console.WriteLine($"frame: {world.Frame}");
        //if (world.Frame == 2000)//350)
        //{
        //    writeStream.Flush();
        //    //System.Diagnostics.Debugger.Break();
        //}
    }

    private StreamWriter writeStream;
    private Random random = new Random(987654321);
}